*******************************************
/***Since the well data from 
IHS Markit Energy is proprietory,
we anonymize county and bank ID***/
*******************************************

cd "..."
***********************************
****Institutional-level sample****
clear
use data_rssdid

/*Table 1 Panel A*/
tabstat xboom if year>=2003 & nonboom_bank0==1 & g_dep<., stat(n mean sd p25 p50 p75)
tabstat xboom if year>=2003 & nonboom_bank0==1 & g_dep<. & xboom>0, stat(n mean sd p25 p50 p75)
tabstat g_dep if year>=2003 & nonboom_bank0==1 & g_dep<., stat(n mean sd p25 p50 p75)

************
/*Table 2*/
*1st stage*
reghdfe xboom xboom_iv l_lasset l_lev l_liqs l_cis l_tier1 l_deps if year>=2003 & nonboom_bank0==1 & g_dep<., absorb(rssd_id state_head#year) cluster(rssd_id)
estimates store t1
/*"nonboom_bank0" indicates the bank operates NOT ONLY in areas directly impacted by the shale boom.*/
*2nd stage*
reghdfe g_dep xboom l_lasset l_lev l_liqs l_cis l_tier1 l_deps if year>=2003 & nonboom_bank0==1, absorb(rssd_id state_head#year) cluster(rssd_id)
estimates store t2
ivreghdfe g_dep l_lasset l_lev l_liqs l_cis l_tier1 l_deps (xboom=xboom_iv) if year>=2003 & nonboom_bank0==1, absorb(rssd_id state_head#year) cluster(rssd_id)
estimates store t3
reghdfe g_rd xboom l_lasset l_lev l_liqs l_cis l_tier1 l_deps if year>=2003 & nonboom_bank0==1, absorb(rssd_id state_head#year) cluster(rssd_id)
estimates store t4
ivreghdfe g_rd l_lasset l_lev l_liqs l_cis l_tier1 l_deps (xboom=xboom_iv) if year>=2003 & nonboom_bank0==1, absorb(rssd_id state_head#year) cluster(rssd_id)
estimates store t5
reghdfe g_td xboom l_lasset l_lev l_liqs l_cis l_tier1 l_deps if year>=2003 & nonboom_bank0==1, absorb(rssd_id state_head#year) vce(cluster rssd_id)
estimates store t6
ivreghdfe g_td l_lasset l_lev l_liqs l_cis l_tier1 l_deps (xboom=xboom_iv) if year>=2003 & nonboom_bank0==1, absorb(rssd_id state_head#year) cluster(rssd_id)
estimates store t7
reghdfe g_bd xboom l_lasset l_lev l_liqs l_cis l_tier1 l_deps if year>=2003 & nonboom_bank0==1, absorb(rssd_id state_head#year) cluster(rssd_id)
estimates store t8
ivreghdfe g_bd l_lasset l_lev l_liqs l_cis l_tier1 l_deps (xboom=xboom_iv) if year>=2003 & nonboom_bank0==1, absorb(rssd_id state_head#year) cluster(rssd_id)
estimates store t9

/*Compute the overall R-Squared for each regression above*/
matrix R=J(1,9,0)
forval i=1/9{
cap drop e_sample 
cap drop yy
est restore t`i'
gen e_sample=e(sample)
local rss=e(rss)
local depvar=e(depvar)
cap sum `depvar' if e_sample==1
gen yy=(`depvar'-r(mean))^2
cap total yy if e_sample==1
matrix r=e(b)
matrix R[1,`i']=1-`rss'/r[1,1]
}
cap drop yy
matrix list R


***********************************
****BHC-level sample****
clear
use data_bhc

/*Table 1 Panel A*/
tabstat cost_dep975 if nonboom_bhc0==1 & year>=2003 & year<=2013, stat(n mean sd p25 p50 p75)
tabstat mtb if nonboom_bhc0==1 & year>=2003 & year<=2013, stat(n mean sd p25 p50 p75)

************
/*Table 3*/
/*"nonboom_bbhc0" indicates the BHC operates NOT ONLY in areas directly impacted by the shale boom.*/
reghdfe cost_dep xboom l_lasset l_lev l_liqs l_cis l_tier1 l_deps if nonboom_bhc0==1 & year>=2003 & year<=2013, absorb(bhc state_head#year) cluster(bhc)
est store t1
ivreghdfe cost_dep l_lasset l_lev l_liqs l_cis l_tier1 l_deps (xboom=xboom_iv) if nonboom_bhc0==1 & year>=2003 & year<=2013, absorb(bhc state_head#year) cluster(bhc)
est store t2
reghdfe lmtb xboom l_lasset l_lev l_liqs l_cis l_tier1 l_deps if nonboom_bhc0==1 & year>=2003 & year<=2013, absorb(bhc state_head#year) cluster(bhc)
est store t3
ivreghdfe lmtb l_lasset l_lev l_liqs l_cis l_tier1 l_deps (xboom=xboom_iv) if nonboom_bhc0==1 & year>=2003 & year<=2013, absorb(bhc state_head#year) cluster(bhc)
est store t4

/*Compute the overall R-Squared for each regression above*/
matrix R=J(1,4,0)
forval i=1/4{
cap drop e_sample 
cap drop yy
est restore t`i'
gen e_sample=e(sample)
local rss=e(rss)
local depvar=e(depvar)
cap sum `depvar' if e_sample==1
gen yy=(`depvar'-r(mean))^2
cap total yy if e_sample==1
matrix r=e(b)
matrix R[1,`i']=1-`rss'/r[1,1]
}
cap drop yy
matrix list R
